package com.ctsi.security;


import com.ctsi.framework.common.startrun.ComponentFactory;

import java.util.Set;


public class DataObjectUtil {

	/**
	 * 返回JDBC数据对象查询条件
	 *
	 * @param userLoginId
	 * @param dataObjectType
	 *  @param action
	 * @param field
	 * @return
	 */
	public static String getJdbcSqlBack(String userLoginId, String dataObjectType,String action,
			String field) {
		ISecurity security = (ISecurity) ComponentFactory.getApplicationContext().getBean("iSecurity");
		Set<String> listDataObjectId = security.findDataObjectIds(userLoginId,
				dataObjectType,action);
		StringBuilder sbsql = new StringBuilder();
		if (listDataObjectId.size() == 0) {

		} else if (listDataObjectId.size() == 1) {
			sbsql.append(" " ) .append( field ) .append( "='" ) .append( listDataObjectId.toArray()[0] ) .append( "'");
		} else {
			sbsql.append(" " ) .append( field ) .append( " in(");
			int n=0;
			for(String did :listDataObjectId){
				if(n++!=0){
					sbsql.append(',');	
				}
				sbsql.append("'") .append( did) .append( "'");
			}
//			for (int i = 0; i < listDataObjectId.size(); i++) {
//				if (i == 0) {
//					sbsql.append(",'" + listDataObjectId.get(i) + "'");
//				} else {
//					sbsql.append(",'" + listDataObjectId.get(i) + "'");
//				}
//			}
			sbsql.append(")");
		}
		return sbsql.toString();
	}

	/**
	 * 返回JDBC数据对象查询条件 数据对象ID 默认为"DATAOBJECTID"
	 *
	 * @param userLoginId
	 * @param dataObjectType
     * @param action
	 * @return
	 */
	public static String getJdbcSqlBack(String userLoginId, String dataObjectType,String action) {
		return getJdbcSqlBack(userLoginId, dataObjectType,action, "DATAOBJECTID");
	}



}
